import {snCreateUIDom} from 'src/hrpub/common/utils/utils';
import CommonAction from './common';
import {COMMON} from "../unit/util";
import {addBlackStyle} from "../../../public/functions/unit";


const reqData = [{
    rqUrl: '/platform/templet/querypage.do',
    rqJson: `{\n  \"pagecode\": \"${COMMON.appConfig.pagecode}\",\n  \"appcode\": \"${COMMON.appConfig.appcode}\"\n}`,
    rqCode: 'template'
}, /*{
    rqUrl: '/platform/appregister/queryallbtns.do',
    rqJson: `{\n  \"pagecode\": \"60092050nccloud\",\n  \"appcode\": \"${COMMON.appConfig.appcode}\"\n}`,
    rqCode: 'button'
},*/ {
    rqUrl: '/platform/appregister/queryappcontext.do',
    rqJson: `{\n  \"appcode\": \"${COMMON.appConfig.appcode}\"}`,
    rqCode: 'context'
}];

export default class MainAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        if(comp.props.use){
            comp.props.use.editTable('regdetail');
            comp.props.use.editTable('dimidetail');
            comp.props.use.editTable('entrydetail');
            comp.props.use.editTable('chgdetail');
            comp.props.use.editTable('psndetail', 'ageedudetail', 'rankstrudetail');
        }
    }

    // 初始化
    didMount = () => {
        if (window.location.href.match(/(localhost|127\.0\.0\.1):3006/)) window.location.hash = `#ifr?page=2019`;

        // todo 本地调试时添加 暗黑色主题
        // addBlackStyle();

        this.getTemplate();
        this.getLanguage();
        // 统一订阅事件
        this.subscribe();
    };

    // 卸载
    willUnMount = () => {
        this.pubSub.unSubscribe('getMainTableData');
    };

    // 获取模版信息
    getTemplate = () => {
        const {props} = this.comp;
        const {
            meta,
            button,
            dispatch
        } = props;

        snCreateUIDom(COMMON.appConfig, reqData, (res) => {
            dispatch({
                type: 'staffCom/update',
                payload: {
                    context: res.context
                }
            });
            meta.setMeta(res.template || {});
            button.setButtons(res.button || []);
        });
    };

    // 获取多语
    getLanguage = () => {
        const {props} = this.comp;
        const {
            dispatch,
            MultiInit
        } = props;

        MultiInit.getMultiLang({
            moduleId: 'gx6008',
            domainName: 'hrtrn',
            callback: (json, status, init) => {
                dispatch({
                    type: 'staffCom/update',
                    payload: {
                        language: json
                    }
                });
            }
        });
    };

    // 事件订阅部分
    subscribe = () => {
        const {comp: {action}, pubSub} = this;
        pubSub.subscribe('getMainTableData', (...args) => {
            action.tableAct.getMainTableData(...args);
        });
    };

}
